//
//  GXJPagesViewController.m
//  AppFrame
//
//  Created by GXJ on 2017/2/24.
//  Copyright © 2017年 GXJ. All rights reserved.
//

#import "GXJPagesViewController.h"
#import "GXJTopTitleView.h"

@interface GXJPagesViewController ()<GXJTopTitleViewDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) GXJTopTitleView *topTitleView;
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) NSMutableArray *titles;
@end

@implementation GXJPagesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}
-(void)setOnepageNumber:(NSInteger)onepageNumber
{
    _onepageNumber = onepageNumber;
}
-(void)setControllersArr:(NSArray *)controllersArr
{
    _controllersArr = controllersArr;
    self.titles = [[NSMutableArray alloc]init];
    // 1.添加所有子控制器
    for(int i = 0;i<_controllersArr.count ; i++)
    {
        [self addChildViewController:_controllersArr[i]];
        [self.titles addObject:[_controllersArr[i] title]];
    }
    
    self.topTitleView = [GXJTopTitleView topTitleViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    
    _topTitleView.showNumber = _onepageNumber;//首页显示按钮的个数
    
    _topTitleView.delegate_GXJ = self;
    
    CALayer *bottomLayer = [CALayer layer];
    bottomLayer.backgroundColor = [UIColor lightGrayColor].CGColor;
    bottomLayer.frame = CGRectMake(0 ,CGRectGetMaxY(_topTitleView.frame)-1, self.view.frame.size.width/_onepageNumber*_titles.count, 1.0);
    [_topTitleView.layer addSublayer:bottomLayer];
    
    [self.view addSubview:_topTitleView];
    
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, CGRectGetMaxY(self.topTitleView.frame), self.view.frame.size.width, self.view.frame.size.height-CGRectGetMaxY(self.topTitleView.frame)-64);
    
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * _titles.count, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = NO;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _mainScrollView.delegate = self;
    
    [self.view addSubview:_mainScrollView];
    
}
-(void)setTitleType:(TitleType)titleType
{
    _titleType = titleType;
    switch (_titleType) {
        case STATICTYPE:
            _topTitleView.staticTitleArr = [NSArray arrayWithArray:_titles];
            break;
            
        case SCROLLTYPE:
            _topTitleView.scrollTitleArr = [NSArray arrayWithArray:_titles];
            break;
            
        default:
            break;
    }
}
//点击头部
-(void)GXJTopTitleView:(GXJTopTitleView *)topTitleView didSelectTitleAtIndex:(NSInteger)index
{
    // 1 计算滚动的位置
    CGFloat offsetX = index * self.view.frame.size.width;
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 2.给对应位置添加对应子控制器
    [self showVc:index];
}
// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.mainScrollView.frame.size.height);
}

//手势滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    [_topTitleView scrollTitleLabelScrollCenter:index];
    
    // 1.添加子控制器view
    [self showVc:index];
    
    // 2.把对应的标题选中
    UILabel *selLabel = self.topTitleView.allTitleLabel[index];
    
    // 3.滚动时，改变标题选中
    [self.topTitleView staticTitleLabelSelecteded:selLabel];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
